clear all
cd "..." //windows
use quarterly_data.dta, clear
global yvarlist SalesExpR FinExpR AdmExpR AstTurnover ROE ROA
*按照年份季度生成政策时间虚拟变量
gen post = yquarter> tq(2020Q2) 
*按照"三条红线"政策生成样本实验组虚拟变量
gen treat_lev = (LtoAexAd>70) if yquarter> tq(2020Q2)
gen treat_netDtoE = (netDtoE>100) if yquarter> tq(2020Q2) 
gen treat_cash_STD = (cash_STD>100) if yquarter> tq(2020Q2) 
*按照2019年财务数据将红档与橙档房企作为实验组
gen treat_2020q2 = (treat_lev+treat_netDtoE+treat_cash_STD>=2) if yquarter==tq(2020Q2) 
egen treat = max(treat_2020q2), by (company)
*定义相关交乘项
gen treat_post = treat*post //treat与post交乘项 DiD效应
gen treat_post_SOE = treat*post*SOE //定义DDD效应的交乘项

*DiD回归-三道红线 Panel A
xtreg logRev treat_post treat_post_SOE AssetsGrR LtoAexAd STDitoDi ///
	AccRTurnover i.yquarter, fe r
	est store reg_logRev

xtreg AssetsGrR treat_post treat_post_SOE logRev LtoAexAd STDitoDi AccRTurnover ///
	i.yquarter, fe r
	est store reg_AssetsGr

foreach yvar in $yvarlist{
	xtreg `yvar' treat_post treat_post_SOE l.LtoAexAd LtoAexAd AccRTurnover ///
	logRev AssetsGrR STDitoDi ///
	i.yquarter, fe r
	est store reg_`yvar'
}
**结果汇总
esttab reg_logRev reg_AssetsGr reg_SalesExpR reg_FinExpR reg_AdmExpR ///
	reg_AstTurnover reg_ROE reg_ROA, ///
	b(%9.3f) star(* 0.1 ** 0.05 *** 0.01) se scalars(N r2 F p) mtitles ///
	title("三道红线DiD回归"), ///
	using results.csv,replace

*DiD回归-疫情影响 Panel B  2017Q1~2020Q2
gen postcovid = (yquarter >= tq(2020Q1)) 
gen treat_postcovid = treat*postcovid //treat与post交乘项 DiD效应
gen treat_postcovid_SOE = treat*postcovid*SOE //定义DDD效应的交乘项

xtreg logRev treat_postcovid treat_postcovid_SOE AssetsGrR LtoAexAd STDitoDi ///
	AccRTurnover i.yquarter if yquarter<=tq(2020Q2), fe r
	est store reg2_logRev

xtreg AssetsGrR treat_postcovid treat_postcovid_SOE logRev LtoAexAd STDitoDi ///
	AccRTurnover i.yquarter if yquarter<=tq(2020Q2), fe r
	est store reg2_AssetsGr

foreach yvar in $yvarlist{
	xtreg `yvar' treat_postcovid treat_postcovid_SOE l.LtoAexAd LtoAexAd ///
	AccRTurnover logRev AssetsGrR STDitoDi ///
	i.yquarter if yquarter<=tq(2020Q2), fe r
	est store reg2_`yvar'
}
	
**结果汇总
esttab reg2_logRev reg2_AssetsGr reg2_SalesExpR reg2_FinExpR reg2_AdmExpR ///
	reg2_AstTurnover reg2_ROE reg2_ROA, ///
	b(%9.3f) star(* 0.1 ** 0.05 *** 0.01) se scalars(N r2 F p) mtitles ///
	title("疫情影响DiD回归 2017Q1~2020Q1"), ///
	using results2.csv,replace











